package paf.ui;

import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import paf.model.Connection;
import paf.model.DiagramExpressionModel;
import paf.model.Observable;
import paf.model.UseCase;

public class MatrixView extends JPanel implements Observer{

	private DiagramExpressionModel dem;
	private static MatrixView mv;
	
	public MatrixView(){
		dem = DiagramExpressionModel.get();
		dem.addObserver(this);
		
		renderView();
	}
	
	public static MatrixView get() {
		if(mv == null) mv = new MatrixView();
		return mv;
	}

	public void renderView(){
		this.removeAll();
		
		//Fetch use cases, classes and connections
		ArrayList<UseCase> allUseCases = dem.getUseCases();
		ArrayList<paf.model.Class> allClasses = dem.getClasses();
		ArrayList<Connection> allConnections = dem.getConnections();

		//Find the largest string (headers are resized accordingly)
		int largestStringLength = 0;

		for(UseCase u : allUseCases) {
			if (largestStringLength < u.getName().length())
				largestStringLength = u.getName().length();
		}

		//Put all use cases in a String array
		final String[] rowHeaders = new String[allUseCases.size()];

		int rowListCounter = 0;

		for(UseCase u : allUseCases) {
			rowHeaders[rowListCounter] = u.getName();
			rowListCounter++;
		}

		//put all classes in a String array
		final String[] columnHeaders = new String[allClasses.size()];

		int columnListCounter = 0;

		for(paf.model.Class c : allClasses) {
			String naam = c.getName();
			columnHeaders[columnListCounter] = naam;
			columnListCounter++;
		}

		Object[][] data = new Object[allUseCases.size()][allClasses.size()];

		//Go through all connections, then go through all classes and use cases and check for connections.class = class and connections.usecase = usecase, then set the CRUD on the diagram
		for(Connection c : allConnections) {
			String connectionClassName = c.getConnectedClass().getName();
			String connectionUseCaseName = c.getConnectedUseCase().getName();
			for(String cl : columnHeaders) {
				for(String use : rowHeaders) {
					if(connectionClassName == cl.toString() && connectionUseCaseName == use) {
						String crud = "";
						if(c.isCreate())
							crud = crud + "C";
						if(c.isRead())
							crud = crud + "R";
						if(c.isUpdate())
							crud = crud + "U";
						if(c.isDelete())
							crud = crud + "D";
						data[Arrays.asList(rowHeaders).indexOf(use)][Arrays.asList(columnHeaders).indexOf(cl)] = crud;
						crud = "";
					}
				}
			}
		}

		//Create JTable
		JTable table = new JTable(data, columnHeaders);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		//Create rowheaders and set size
		JList rowHeader = new JList(rowHeaders);
		rowHeader.setFixedCellHeight(19);
		rowHeader.setFixedCellWidth(largestStringLength * 7);
		rowHeader.setCellRenderer(new RowHeaderRenderer(table));
		
		//Set row heights
		table.setRowHeight(19);

		JScrollPane scroll = new JScrollPane(table);
		scroll.setRowHeaderView(rowHeader);
		
		this.setSize(600, 600);
this.setVisible(true);
		this.add(scroll);
	}

	@Override
	public void update(Observable o) {
		System.out.println("Matrix updated");
		renderView();
	}
}
